1 00:00:00,680 --> 00:00:02,630 Thank you for joining me in this lecture. 2 00:00:02,630 --> 00:00:07,430 We're going to be taking a look at an object oriented design technique called composition. 3 00:00:07,820 --> 00:00:13,340 Composition is a way for us to create complex objects and systems by combining simpler objects into 4 00:00:13,340 --> 00:00:14,210 one object. 5 00:00:14,570 --> 00:00:20,390 The main idea of composition is to have an object like, let's say, a car, exist with separate objects 6 00:00:20,390 --> 00:00:22,820 that each behave and work on their own functionality. 7 00:00:23,120 --> 00:00:27,920 As an example, if we were to create a car object, we would want separate objects within the car object 8 00:00:27,920 --> 00:00:32,150 to do their own thing like the tires, the suspension, the engine, and so on. 9 00:00:32,210 --> 00:00:37,340 The style of building complex objects can be easier to understand and is actually preferred by many 10 00:00:37,340 --> 00:00:38,990 developers over inheritance. 11 00:00:38,990 --> 00:00:41,750 So let's take a look at how we can use composition in Lua. 12 00:00:42,380 --> 00:00:47,510 I'm going to create a class, and this class is simply just going to represent a 2D coordinate. 13 00:00:47,510 --> 00:00:49,520 So we'll just call it coordinate. 14 00:00:50,740 --> 00:00:54,850 And a coordinate needs an X property and it needs a Y property. 15 00:00:55,930 --> 00:01:00,550 And we'll make sure to set the coordinate underscore underscore index meta method to the coordinate 16 00:01:00,550 --> 00:01:01,390 class. 17 00:01:01,390 --> 00:01:04,900 And then we can create a constructor for our coordinate class. 18 00:01:06,680 --> 00:01:10,910 Pass an X and a Y position, and all we need to do is create a new table. 19 00:01:10,910 --> 00:01:12,410 I'm going to call it self. 20 00:01:12,530 --> 00:01:13,250 It's going to be equal. 21 00:01:13,250 --> 00:01:14,270 To set meta table. 22 00:01:14,270 --> 00:01:19,250 We're going to create a new table in here and attach the coordinate class to this table. 23 00:01:19,370 --> 00:01:23,150 Then from that point we can set the x equal to the x pass to the function. 24 00:01:23,150 --> 00:01:26,420 And we can set the y equal to the y pass to the function. 25 00:01:26,420 --> 00:01:28,490 And then we can just return self. 26 00:01:29,490 --> 00:01:32,340 Now I'm going to include one function in this class. 27 00:01:34,640 --> 00:01:37,100 And this function is going to be called draw. 28 00:01:37,910 --> 00:01:40,100 And all it's going to do is print. 29 00:01:40,100 --> 00:01:46,100 Drawing coordinates at x will give the x coordinate. 30 00:01:48,690 --> 00:01:51,750 And then we'll do it at the Y coordinate as well. 31 00:01:55,120 --> 00:01:56,740 Now let's create a second class. 32 00:01:56,740 --> 00:02:02,650 And this class I'm just going to call it complex object, just to represent our complex object. 33 00:02:04,330 --> 00:02:08,050 And inside of this class we're going to have a property called components. 34 00:02:09,280 --> 00:02:14,890 And in here we're going to store as many of these coordinate objects as we'd like inside of our complex 35 00:02:14,890 --> 00:02:15,670 object. 36 00:02:17,020 --> 00:02:21,670 And we're going to make sure to set the underscore underscore index meta method equal to the complex 37 00:02:21,670 --> 00:02:22,840 objects table. 38 00:02:23,380 --> 00:02:26,500 Let's also create a constructor for this complex object. 39 00:02:30,270 --> 00:02:34,530 And now we're going to do is return the table from set meta table. 40 00:02:34,530 --> 00:02:35,910 We're going to create a new table. 41 00:02:35,910 --> 00:02:40,860 And we're going to set the complex object class as the meta table. 42 00:02:42,240 --> 00:02:44,790 And then we can add a function to this class. 43 00:02:47,610 --> 00:02:49,740 Like adding a coordinate. 44 00:02:51,620 --> 00:02:53,000 To the components table. 45 00:02:53,000 --> 00:02:55,400 So we'll pass a coordinate to this function. 46 00:02:55,400 --> 00:03:03,020 And all this function is going to do is insert into the components property this coordinate. 47 00:03:04,270 --> 00:03:10,420 And now we can have a function that draws all the coordinates that are inside of the components table. 48 00:03:10,420 --> 00:03:12,550 So we can do complex object. 49 00:03:13,710 --> 00:03:15,330 Draw coordinates. 50 00:03:16,150 --> 00:03:21,340 And all this function is going to do is loop through every single coordinate that is in the components 51 00:03:21,340 --> 00:03:22,150 table. 52 00:03:25,010 --> 00:03:26,900 And it's going to. 53 00:03:27,830 --> 00:03:33,500 Call the draw function on that coordinate, which belongs to the coordinate class. 54 00:03:35,040 --> 00:03:39,270 So now what we could do is we could create a new complex object. 55 00:03:42,990 --> 00:03:46,110 And then we can insert some coordinates inside of the complex object. 56 00:03:46,110 --> 00:03:47,640 So a complex object. 57 00:03:48,810 --> 00:03:53,580 And then we can call the add coordinate function and create a new coordinate. 58 00:03:54,660 --> 00:03:57,000 We'll just set this one to maybe like three and 11. 59 00:03:58,720 --> 00:04:01,000 And then we could do another one. 60 00:04:03,210 --> 00:04:04,140 Create a new coordinate. 61 00:04:04,140 --> 00:04:07,320 And for this one we'll just do like nine and negative four. 62 00:04:09,520 --> 00:04:12,040 And then when we go to call the. 63 00:04:13,420 --> 00:04:15,190 Draw coordinates function. 64 00:04:15,370 --> 00:04:19,870 It should print three and 11 and nine and four in the console. 65 00:04:20,990 --> 00:04:26,690 So what's going on here is that we're creating a new complex object from our complex object class, 66 00:04:26,690 --> 00:04:33,050 and we're just inserting other objects that came from a different class into our complex object, our 67 00:04:33,050 --> 00:04:33,980 complex objects. 68 00:04:33,980 --> 00:04:38,420 Kind of vague, but I'm just kind of giving you an example of how composition works. 69 00:04:38,420 --> 00:04:43,520 You could also imagine our complex object as a car and us we're inserting, you know, the different 70 00:04:43,520 --> 00:04:48,050 objects to represent maybe the tires or the engines and things like that. 71 00:04:48,800 --> 00:04:54,470 So once we insert all of these new coordinate objects inside of our complex object, they get stored 72 00:04:54,470 --> 00:04:56,180 within the components table. 73 00:04:56,180 --> 00:05:00,950 And when we call the draw coordinates function, it's going to go through each one of those coordinate 74 00:05:00,950 --> 00:05:05,990 objects and call the draw function on them which exists within the coordinate table. 75 00:05:05,990 --> 00:05:11,660 So it should print each of the coordinates that belong to our complex object. 76 00:05:11,660 --> 00:05:14,810 And let me make sure to fix that real quick right there. 77 00:05:14,840 --> 00:05:17,510 Got to make sure to concatenate to the string. 78 00:05:18,240 --> 00:05:23,820 So if we go and run our game, we should get two statements printed into the console. 79 00:05:25,500 --> 00:05:34,290 And there we go drawing coordinates at x three, y 11 and drawing coordinates at x nine and y negative 80 00:05:34,290 --> 00:05:34,980 four. 81 00:05:35,750 --> 00:05:40,580 And we could also do a for loop for I equals 1 to 100. 82 00:05:40,580 --> 00:05:46,760 What we'll do is we'll create a bunch of new coordinates to insert into our table. 83 00:05:47,480 --> 00:05:50,840 So we can just do something like math dot random. 84 00:05:51,350 --> 00:05:56,090 And or actually what we'll do is we'll use random dot new. 85 00:05:56,940 --> 00:05:59,580 And we'll get an integer between. 86 00:06:00,540 --> 00:06:00,900 I don't know. 87 00:06:00,900 --> 00:06:03,510 -1001 thousand. 88 00:06:04,990 --> 00:06:07,210 And we'll do the same thing here for the y coordinate. 89 00:06:07,210 --> 00:06:12,640 After we generate a bunch of different coordinates, then it should print 100 different statements into 90 00:06:12,640 --> 00:06:13,390 the console. 91 00:06:13,390 --> 00:06:14,620 So if we run it. 92 00:06:16,290 --> 00:06:16,890 There we go. 93 00:06:16,890 --> 00:06:21,840 We get 100 different coordinates that are stored in our complex object. 94 00:06:23,470 --> 00:06:28,450 So as you can see, composition can be a lot easier to understand than inheritance. 95 00:06:28,450 --> 00:06:33,760 Instead of inheriting classes into other classes, we're just putting objects into other objects. 96 00:06:33,760 --> 00:06:36,130 That's just kind of the main idea of composition. 97 00:06:36,130 --> 00:06:37,780 Nothing too complex about it. 98 00:06:37,780 --> 00:06:44,110 It just allows us to break down complex objects into smaller and smaller, more simpler objects, like 99 00:06:44,110 --> 00:06:49,090 having a car object, and then you can break it down into its other components like the doors, the 100 00:06:49,090 --> 00:06:51,820 tires, the engine, the spring, and so on. 101 00:06:52,150 --> 00:06:54,910 So I hope you enjoyed this lecture and I'll see you in the next one.